package com.googlecode.droidircclient;

import android.content.Context;
import java.util.Vector;
import java.io.*;

public class Profile {

	/*The first value of 'targets' specifies a host/server; 
	 * the second specifies channels on that server.
	 * think of it as a spreadsheet:
	 * 
	 * Host1   |Host2    |Host3    
	 * ----------------------------
	 * Channel1|Channel1|Channel1
	 * Channel2|		|Channel2
	 * Channel3|        |
	 * 
	 * 
	 */
	private Vector<Vector<String>> targets= new Vector<Vector<String>>();
	//names[0] = name; names[1] = real name; names[2] = nick
	private String[] names ={"Nobody", "Nobody", "Nobody"};
	//"root" is the Context that originally asked for the profile.
	//This should not be any class other than our "DroidIRCclientActivity."
	private Context root;
	private final String PROFILE_NAME = ".droidIRC";
	
	public Profile(Context root){
		this.root = root;
		System.out.println("\"Profile\" Constructor invoked!");
		writeProfile();
		try{
			BufferedReader profileScanner = new BufferedReader(new InputStreamReader( root.getResources().openRawResource(R.raw.profile)));
			String line = null;
			int count = 0;
			while(line == null && count < 20){
				line = profileScanner.readLine(); 
				System.out.println(line + " read.");
				++count;
			}
			/*Below is parsing.*/
			int curr = 0;
			System.out.println("Line in: " + line);
			while(curr < line.length()){
				int stop = line.indexOf('$', curr);
				String linePiece = line.substring(curr, stop);
				int k;
				for(k=0;linePiece.charAt(k) != '&' && linePiece.charAt(k) != '%' && linePiece.charAt(k) != '*';++k);
				int number = Integer.parseInt(linePiece.substring(0,k));
				
				if(linePiece.charAt(k) == '&'){//add server
					while(targets.size() < (number+1)) targets.add(new Vector<String>());
					if(targets.elementAt(number).size()==0) targets.elementAt(number).add("");
					targets.elementAt(number).setElementAt((linePiece.substring(k+1, linePiece.length())),0);
				}
				if(linePiece.charAt(k) == '%'){//add channel
					if(targets.elementAt(number).size() < 1) targets.elementAt(number).add(new String("None"));
					targets.elementAt(number).add(linePiece.substring(k+1, linePiece.length()));
				}
				if(linePiece.charAt(k) == '*'){//add name
					if(number == 1){
						names[0] = (linePiece.substring(k+1, linePiece.length()));
						System.out.println("Name:" + names[0]);
					}
					if(number == 2){
						names[1] = (linePiece.substring(k+1, linePiece.length()));
						System.out.println("Real Name: " + names[1]);
					}
					if(number == 3){
						names[2] = (linePiece.substring(k+1, linePiece.length()));
						System.out.println("Nick: " + names[2]);
					}
				}
				curr = stop+1;
				
		}//while(still parsing)
			
			
		}catch(FileNotFoundException e){
			try{
				//this will create the file if it does not exist.
				root.openFileOutput(PROFILE_NAME, Context.MODE_PRIVATE);
				//write default profile values
				writeProfile();
			}
			catch(IOException ex){
				System.err.println("IOException: could not create profile.");
				e.printStackTrace();
			}
		}		

		catch(IOException ee){
			System.err.println("IOException: Cannot write to profile.");
			System.err.println(ee.toString());
		}//parse profile
		writeProfile();
	}//constructor

	//return all servers in profile
	public String[][] getHosts(){
		int j, k;
		String[][] returner = new String[targets.size()][1];
		for(j=0;j<targets.size();++j){
			returner[j] = new String[targets.elementAt(j).size()];
			int size = targets.elementAt(j).size();
			for(k = 0; k<size;++k){
				returner[j][k] = targets.elementAt(j).elementAt(k);
			}
		}
		return returner;
	}
	
	//return names in profile
	public String[] getNames(){
		int j;
		String[] returner = new String[3];
		for(j=0;j<3;++j){
			if(names[j] == null) returner[j] = "Nobody";
			else returner[j] = names[j];
		}
		return returner;
	}
	
	public boolean storeProfile(){
		return writeProfile();
	}
	
	/*
	 * Writes current profile to file. This should happen every time the
	 * program exits, to save changes to the profile.
	 * Returns true if file was written successfully.
	 */
	public boolean writeProfile(){
		System.out.print("writing profile...");
		int j, k;
		StringBuffer line = new StringBuffer();
		try{
			OutputStreamWriter profiler = new OutputStreamWriter( root.openFileOutput(PROFILE_NAME, Context.MODE_PRIVATE));
			for(j=0;j<targets.size();++j){
				line.append( j + "&" + targets.elementAt(j).elementAt(0) + "$");
				for(k=0;k<targets.elementAt(j).size();++k){
					line.append( j + "#" + targets.elementAt(j).elementAt(k));
				}
			}
			for(j=0;j<3;++j)
				line.append(j + "*" + names[j] + "$");
			profiler.write(line.toString(), 0, line.length());
			System.out.println("\"" + line.toString() + "\"" + " written.");
		} 
		catch(IOException e){
			System.err.println("IOException: Could not open profile for writing.");
			System.err.println(e.toString());
			return false;
		}
		return true;
	}//writePrifile(File)

}
